from bs4 import BeautifulSoup

# 复杂HTML示例
complex_html = """
<html>
<body>
<div id="main">
    <h1>标题</h1>
    <p class="content">第一段<strong>重点</strong>内容</p>
    <!-- 这是注释 -->
    <p>第二段内容</p>
</div>
</body>
</html>
"""

soup = BeautifulSoup(complex_html, 'lxml')

# 1. 获取BeautifulSoup对象
print("=== BeautifulSoup对象 ===")
print(f"文档类型: {type(soup)}")

# 2. 获取Tag对象
div_tag = soup.find('div', id='main')
print("\n=== Tag对象 ===")
print(f"标签名: {div_tag.name}")
print(f"标签属性: {div_tag.attrs}")

# 3. 遍历Tag对象的子节点
print("\n=== 遍历子节点 ===")
for i, child in enumerate(div_tag.children):
    print(f"子节点 {i}: 类型={type(child).__name__}, 内容={repr(str(child)[:30])}")

# 4. 处理NavigableString
print("\n=== NavigableString ===")
first_p = div_tag.find('p')
print(f"段落文本: {repr(first_p.text)}")

# 5. 处理混合内容
print("\n=== 混合内容分析 ===")
for element in first_p.contents:
    element_type = type(element).__name__
    content = repr(str(element).strip())
    print(f"元素: {element_type:15} -> {content}")